import { _decorator, CCBoolean, Component, Node ,sys} from 'cc';
const { ccclass, property,executionOrder } = _decorator;
import { delay } from '../global/Func';





const key:string="dataKey";

@ccclass('SaveMgr')
@executionOrder(-1)
export class SaveMgr extends Component {

    private static inst:SaveMgr|null=null;

    @property(CCBoolean)
    clear:boolean=false;

   
    public static get Inst(){
        return SaveMgr.inst;
    }

    public Data={
        money:0,
        musicVal:0,
        effectVal:0,
    }

    protected onLoad(): void {
       if(SaveMgr.inst==null){
        SaveMgr.inst=this;
       }

       if(this.clear){
        this.clearData();
       }
       this.loadData();

       //this.test();
    }

    private test(){
        this.printData();
        this.Data.money+=1;
        this.saveData();
    }

    private clearData(){
        sys.localStorage.removeItem(key);
    }

    private loadData(){
       var content= sys.localStorage.getItem(key);

       if(content==null){
            sys.localStorage.setItem(key,JSON.stringify(this.Data));
       }else {
            this.Data=JSON.parse( sys.localStorage.getItem(key));
       }

    }

    public saveData(){
        sys.localStorage.setItem(key,JSON.stringify(this.Data));
        console.log("saveData");
    }

    public printData(){
        console.log(this.Data);
    }

}


